package cn.chint.manage.dao.impl;

import cn.chint.manage.dao.ReviewerDao;
import cn.chint.po.Review;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class ReviewerDaoImpl implements ReviewerDao {
    @Autowired
    HibernateTemplate hibernateTemplate;
    @Override
    public int inst(final String name_wx){
//        Review review = new Review();
//        review.setName_wx(name_wx);
//        String hql ="SELECT wx_open_id FROM t_user WHERE wx_nickname = '"+name_wx+"'";
//        review.setOpen_wx_id(hql);
        //这个应该怎么改？  看下语句 可以这样写吗？
        int flag = (int) this.hibernateTemplate.execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                return session.createSQLQuery("Insert into review(name_wx,open_wx_id) values('"+name_wx+"'," +
                        "(select wx_open_id from t_user where wx_nickname = '"+name_wx+"'))").executeUpdate();
            }
        });
        return flag;
    }
    @Override
    public List<Review> sel(){
        List<Review> list = (List<Review>) hibernateTemplate.find("FROM Review ");//不是这个


        return list;
    }
}
